package com.ethink.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.warehouse.api.info.MateProduceCodeInfo;
import com.ethink.warehouse.api.request.MateProduceCodeAddRequest;
import com.ethink.warehouse.api.request.MateProduceCodeQueryRequest;
import com.ethink.warehouse.api.request.MateProduceCodeUpdateRequest;
import com.ethink.warehouse.api.response.MateProduceCodeAddResponse;
import com.ethink.warehouse.api.response.MateProduceCodeDeleteResponse;
import com.ethink.warehouse.api.response.MateProduceCodeUpdateResponse;
import com.ethink.warehouse.common.entity.MateProduceCode;
import com.ethink.warehouse.common.mapper.MateProduceCodeMapper;
import com.ethink.warehouse.converter.MateProduceCodeConverter;
import com.ethink.warehouse.service.MateProduceCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品生产批次号 Service接口实现
 *
 * @author default
 * @date 2022-11-22
 */
@Slf4j
@Service
public class MateProduceCodeServiceImpl implements MateProduceCodeService {

    @Autowired
    private MateProduceCodeMapper mateProduceCodeMapper;

    @Autowired
    private MateProduceCodeConverter mateProduceCodeConverter;

    @Override
    public Page<MateProduceCodeInfo> queryMateProduceCode(MateProduceCodeQueryRequest request) {
        return mateProduceCodeMapper.queryMateProduceCode(PageUtils.buildPage(request), request);
    }

    @Override
    public MateProduceCodeInfo getMateProduceCodeById(Long mateProduceCodeId) {
        return mateProduceCodeMapper.getMateProduceCodeById(mateProduceCodeId);
    }

    @Override
    public List<MateProduceCodeInfo> listMateProduceCode(MateProduceCodeQueryRequest request) {
        return mateProduceCodeMapper.listMateProduceCode(request);
    }

    @Override
    public MateProduceCodeAddResponse addMateProduceCode(MateProduceCodeAddRequest request) {
        MateProduceCodeAddResponse response = new MateProduceCodeAddResponse();
        MateProduceCode mateProduceCode = mateProduceCodeMapper.selectOne(
                new LambdaQueryWrapper<MateProduceCode>()
                        .eq(MateProduceCode::getIsDeleted, false)
                        .eq(MateProduceCode::getMateSku, request.getMateSku())
                        .eq(MateProduceCode::getProduceCode, request.getProduceCode())
        );
        if (mateProduceCode == null) {
            mateProduceCode = new MateProduceCode();
            BeanUtils.copyProperties(request, mateProduceCode);
            mateProduceCodeMapper.insert(mateProduceCode);
        }
        return response;
    }

    @Override
    public MateProduceCodeUpdateResponse updateMateProduceCode(MateProduceCodeUpdateRequest request) {
        MateProduceCodeUpdateResponse response = new MateProduceCodeUpdateResponse();
        LambdaUpdateWrapper<MateProduceCode> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateProduceCode::getId, request.getId());
        // FIXME 按实际业务需求set
        updateWrapper.set(MateProduceCode::getMateSku, request.getMateSku());
        updateWrapper.set(MateProduceCode::getProduceCode, request.getProduceCode());
        updateWrapper.set(MateProduceCode::getProductDate, request.getProductDate());
        updateWrapper.set(MateProduceCode::getExpireDate, request.getExpireDate());
        mateProduceCodeMapper.update(null, updateWrapper);
        return response;
    }

    @Override
    public MateProduceCodeDeleteResponse deleteMateProduceCode(Long mateProduceCodeId) {
        MateProduceCodeDeleteResponse response = new MateProduceCodeDeleteResponse();
        LambdaUpdateWrapper<MateProduceCode> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateProduceCode::getId, mateProduceCodeId);
        updateWrapper.set(MateProduceCode::getIsDeleted, true);
        mateProduceCodeMapper.update(null, updateWrapper);
        return response;
    }

    @Override
    public List<MateProduceCodeInfo> skuAndProduceCodeUnionQuery(List<MateProduceCodeQueryRequest> requestList) {
        return mateProduceCodeMapper.skuAndProduceCodeUnionQuery(requestList);
    }

}
